#!/usr/bin/python3
# @Time    : 2018/6/3 上午 7:29:24  
# @Author  : LaoJI
from . import web
from flask import render_template, jsonify, request,abort
from app.models.teacher import Teacher
from app.models.myclass import MyClass
from app.models.exams import Exam
from app.models.exam_scores import Exam_Score
from app.models.user import User
from flask_login import current_user, login_required


@web.route('/classes', methods=['Get'])
@login_required
def classes():
    # 需要 教师名字
    res = {}
    uid = current_user.get_id()
    result = MyClass.getAllClass(uid)
    uid = current_user.get_id()
    user = User.query.get(int(uid))
    res["data"] = {
        "nickname": user.nickname,
        "id": user.id,
        "avatar_url": user.avatar_url
    }
    res["class"] = result
    return render_template("classes.html", res=res)


@web.route('/class/add', methods=['POST'])
@login_required
def addclass():
    res = {}
    uid = current_user.get_id()
    cid = request.form['classid']
    result = MyClass.addClass(cid,uid)
    if result:
        res['succeed'] = True
        res['message'] = "加入成功"
        return jsonify(res)
    res['succeed'] = False
    res['message'] = "您已经加入了"
    return jsonify(res)




@web.route('/class/check', methods=['POST'])
@login_required
def checkclass():
    res = {}
    # print(request.json,request.form)
    invitecode = request.form['invitecode']
    myclass = MyClass.get_class_by_code(invitecode)
    if myclass:
        res['succeed'] = True
        res['message'] = "验证成功"
        res['class'] = {
            "cid": myclass.class_id,
            "cname": myclass.class_name,
            "cdes": myclass.class_descri,
            "classimg":str("/statics/img/"+myclass.class_logo)
        }
        return jsonify(res)
    res['succeed'] = False
    res['message'] = "没有此班级"
    return jsonify(res)

